﻿namespace AntdTest.Helpers
{
    /// <summary>
    /// AntdUI 帮助类
    /// </summary>
    public class Antd
    {
        readonly static HashSet<AntdUI.Window> subWindows = [];

        /// <summary>
        /// 设置主题
        /// </summary>
        /// <param name="dark"></param>
        public static void SetTheme(AntdUI.Window window, AntdUI.Button btnSwitch, bool dark, Color primary)
        {
            AntdUI.Config.IsDark = dark;
            if (dark)
            {
                btnSwitch.ImageSvg = Properties.Resources.svg_theme_dark;
                window.BackColor = Color.Black;
                window.ForeColor = Color.White;
            }
            else
            {
                btnSwitch.ImageSvg = Properties.Resources.svg_theme_light;
                window.BackColor = Color.White;
                window.ForeColor = Color.Black;
            }
            AntdUI.Style.Db.SetPrimary(primary);
            window.Refresh();
            BroadcastThemeToSubWindows();
            Properties.Settings.Default.ThemeDark = dark;
            Properties.Settings.Default.ThemeColor = primary;
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// 设置主题色
        /// </summary>
        /// <param name="primary"></param>
        public static void SetThemeColor(AntdUI.Window window, Color primary)
        {
            AntdUI.Style.Db.SetPrimary(primary);
            window.Refresh();
            BroadcastThemeToSubWindows();
            Properties.Settings.Default.ThemeColor = primary;
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// 继承主窗体的主题
        /// </summary>
        public static void InheritTheme(AntdUI.Window window)
        {
            bool themeChanged = false;
            bool dark = AntdUI.Config.IsDark;
            if (dark)
            {
                if (window.BackColor != Color.Black)
                {
                    window.BackColor = Color.Black;
                    window.ForeColor = Color.White;
                    themeChanged = true;
                }
            }
            else
            {
                if (window.BackColor != Color.White)
                {
                    window.BackColor = Color.White;
                    window.ForeColor = Color.Black;
                    themeChanged = true;
                }
            }
            if (themeChanged) window.Refresh();
            subWindows.Add(window);
        }

        /// <summary>
        /// 把主题应用到所有子窗体
        /// </summary>
        private static void BroadcastThemeToSubWindows()
        {
            foreach (AntdUI.Window subWin in subWindows)
            {
                if (subWin != null && !subWin.IsDisposed && subWin.Visible)
                {
                    InheritTheme(subWin);
                    subWin.Refresh();
                }
            }
        }

        /// <summary>
        /// 加载器
        /// </summary>
        public class Loading
        {
            /// <summary>
            /// 开始加载
            /// </summary>
            /// <param name="parent">父窗体，可为 null</param>
            /// <param name="caption">加载器文本</param>
            public static void Start(Form parent, string caption = "加载中，请稍候...")
            {
                var frm = Forms.Loading.Instance;
                frm.Opacity = 0.9F;
                if (parent == null)
                {
                    frm.StartPosition = FormStartPosition.CenterScreen;
                }
                else
                {
                    frm.StartPosition = FormStartPosition.Manual;
                    frm.Left = parent.Left + parent.Width / 2 - frm.Width / 2;
                    frm.Top = parent.Top + parent.Height / 2 - frm.Height / 2;

                    #region 位置修正
                    Point posDragEnd = Cursor.Position;
                    Screen screen = Screen.FromPoint(posDragEnd);
                    if (frm.Left < 0)
                    {
                        frm.Left = 0;
                    }
                    else if (frm.Left > screen.Bounds.Width - frm.Width)
                    {
                        frm.Left = screen.Bounds.Width - frm.Width;
                    }
                    if (frm.Top < 0)
                    {
                        frm.Top = 0;
                    }
                    else if (frm.Top > screen.Bounds.Height - frm.Height)
                    {
                        frm.Top = screen.Bounds.Height - frm.Height;
                    }
                    #endregion
                }
                frm.SetCaption(caption);
                if (!frm.Visible) frm.Show(parent);
            }

            /// <summary>
            /// 停止加载
            /// </summary>
            /// <param name="parent"></param>
            /// <returns>总耗时（秒）</returns>
            public static double Stop(Form parent)
            {
                double sec = 0;
                var frm = Forms.Loading.Instance;
                if (parent == null)
                {
                    frm.Hide();
                }
                else
                {
                    parent.BeginInvoke(new Action(() =>
                    {
                        frm.Hide();
                    }));
                }
                sec = frm.ElapsedSeconds;
                return sec;
            }
        }
    }
}